package com.zeli.util;

/**
 * 
 */
public class Order {

	private boolean ascend = true;
	private String field;

	public Order(String field) {
		this(field, true);
	}

	public Order(String field, boolean ascend) {
		this.field = field;
		this.ascend = ascend;
	}

	public Order(String field, String order) {
		this.field = field;
		if (!"asc".equalsIgnoreCase(order)) {
			this.ascend = false;
		}
	}

	public static Order ascend(String field) {
		return new Order(field);
	}

	public static Order descend(String field) {
		return new Order(field, false);
	}

	public boolean isAscend() {
		return ascend;
	}

	public void setAscend(boolean ascend) {
		this.ascend = ascend;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getJPQLExp() {
		if (ascend) {
			return field + " asc";
		} else {
			return field + " desc";
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Order [ascend=");
		builder.append(ascend);
		builder.append(", field=");
		builder.append(field);
		builder.append("]");
		return builder.toString();
	}

}

